package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.domain.ErpProcure;
import com.cleaningcloud.erp.domain.dto.ErpProcureDto;
import com.cleaningcloud.erp.domain.excelExport.ProcureExcelExport;
import com.cleaningcloud.erp.domain.excelExport.WareCheckExcelExport;
import com.cleaningcloud.erp.domain.excelImport.ProcureExcelImport;
import com.cleaningcloud.erp.domain.vo.ErpProcureDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpProcureVo;
import com.cleaningcloud.erp.domain.vo.ErpWareCheckDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWareCheckVo;
import com.cleaningcloud.erp.service.IErpProcureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购单Controller
 *
 * @author cleaningcloud
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/ErpProcure")
public class ErpProcureController extends BaseController {
    @Autowired
    private IErpProcureService erpProcureService;

    /**
     * 查询采购单列表
     */
    @RequiresPermissions("erp:ErpProcure:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpProcureDto erpProcure) {
        startPage();
        List<ErpProcureVo> list = erpProcureService.selectErpProcureList(erpProcure);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @RequiresPermissions("erp:ErpProcure:export")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpProcureDto erpProcure) {
        List<ErpProcureVo> list = erpProcureService.selectErpProcureList(erpProcure);
        ExcelUtil<ErpProcureVo> util = new ExcelUtil<>(ErpProcureVo.class);
        util.exportExcel(response, list, "采购单数据");
    }

    /**
     * 导出采购单明细
     */
    @RequiresPermissions("erp:ErpWareCheck:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{procureId}")
    public void exportDetails(HttpServletResponse response, @PathVariable Long procureId) {
        ErpProcureVo procureVo = erpProcureService.selectErpProcureByProcureId(procureId);
        List<ProcureExcelExport> list = new ArrayList<>();
        for (ErpProcureDetailsVo erpProcureDetailsVo : procureVo.getDetailsList()) {
            ProcureExcelExport procureExcelExport = new ProcureExcelExport();
            procureExcelExport.setProcureNo(procureVo.getProcureNo());
            procureExcelExport.setSupplierName(procureVo.getSupplierName());
            procureExcelExport.setProcureStatus(procureVo.getProcureStatus());
            procureExcelExport.setProcureTime(procureVo.getProcureTime());
            procureExcelExport.setProcurePersonName(procureVo.getProcurePersonName());
            procureExcelExport.setProcurePersonPhone(procureVo.getProcurePersonPhone());
            procureExcelExport.setLogisticsStauts(procureVo.getLogisticsStauts());
            procureExcelExport.setLogisticsCost(procureVo.getLogisticsCost());
            procureExcelExport.setPayAmount(procureVo.getPayAmount());
            procureExcelExport.setApproveName(procureVo.getApproveName());
            procureExcelExport.setApproveTime(procureVo.getApproveTime());
            procureExcelExport.setCreateName(procureVo.getCreateName());
            procureExcelExport.setCommodityNo(erpProcureDetailsVo.getSupplierCommodityNo());
            procureExcelExport.setCommodityName1(erpProcureDetailsVo.getSupplierCommodityName());
            procureExcelExport.setCommodityType(erpProcureDetailsVo.getSupplierCommodityType());
            procureExcelExport.setUnitName1(erpProcureDetailsVo.getSupplierCommodityUnit());
            procureExcelExport.setCommoditySpec(erpProcureDetailsVo.getSupplierCommoditySpec());
            procureExcelExport.setProcurePrice(erpProcureDetailsVo.getProcurePrice());
            procureExcelExport.setProcureNumber(erpProcureDetailsVo.getProcureNumber());
            list.add(procureExcelExport);
        }
        ExcelUtil<ProcureExcelExport> util = new ExcelUtil<>(ProcureExcelExport.class);
        util.exportExcel(response, list, "采购单");
    }

    @Log(title = "采购单", businessType = BusinessType.IMPORT)
    @RequiresPermissions("erp:ErpProcure:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ProcureExcelImport> util = new ExcelUtil<ProcureExcelImport>(ProcureExcelImport.class);
        List<ProcureExcelImport> commodityList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = erpProcureService.importErpProcure(commodityList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ProcureExcelImport> util = new ExcelUtil<ProcureExcelImport>(ProcureExcelImport.class);
        util.importTemplateExcel(response, "采购单");
    }

    /**
     * 获取采购单详细信息
     */
    @RequiresPermissions("erp:ErpProcure:query")
    @GetMapping(value = "/{procureId}")
    public AjaxResult getInfo(@PathVariable("procureId") Long procureId) {
        return success(erpProcureService.selectErpProcureByProcureId(procureId));
    }

    /**
     * 新增采购单
     */
    @RequiresPermissions("erp:ErpProcure:add")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpProcureDto erpProcure) {
        return toAjax(erpProcureService.insertErpProcure(erpProcure));
    }

    /**
     * 修改采购单
     */
    @RequiresPermissions("erp:ErpProcure:edit")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpProcureDto erpProcure) {
        return toAjax(erpProcureService.updateErpProcure(erpProcure));
    }

    /**
     * 删除采购单
     */
    @RequiresPermissions("erp:ErpProcure:remove")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{procureIds}")
    public AjaxResult remove(@PathVariable Long[] procureIds) {
        return toAjax(erpProcureService.deleteErpProcureByProcureIds(procureIds));
    }


    /**
     * 采购单审核
     */
    @RequiresPermissions("erp:ErpProcure:audit")
    @Log(title = "采购单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody ErpProcure erpProcure) {
        return toAjax(erpProcureService.audit(erpProcure));
    }


    /**
     * 更改物流状态
     */
    @RequiresPermissions("erp:ErpProcure:logistics")
    @Log(title = "更改物流状态", businessType = BusinessType.UPDATE)
    @PutMapping("/logistics")
    public AjaxResult logistics(@RequestBody ErpProcure erpProcure) {
        return toAjax(erpProcureService.logistics(erpProcure));
    }

}
